package com.ww.user.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ww.common.dtos.AppHttpCodeEnum;
import com.ww.common.dtos.ResponseResult;
import com.ww.common.exception.LeadNewsException;
import com.ww.model.user.dto.LoginDto;
import com.ww.model.user.pojos.ApUser;
import com.ww.user.mapper.ApUserMapper;
import com.ww.user.service.ApUserService;
import com.ww.utils.common.BCrypt;
import com.ww.utils.common.JwtUtils;
import com.ww.utils.common.RsaUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.security.PrivateKey;
import java.util.HashMap;
import java.util.Map;

@Service
@Transactional
public class ApUserServiceImpl extends ServiceImpl<ApUserMapper, ApUser> implements ApUserService {
    @Value("${leadnews.jwt.privateKeyPath}")
    private String privateKeyPath;//私钥文件路径
    @Value("${leadnews.jwt.expire}")
    private Integer expire;//token的过期时间
    @Override
    public ResponseResult login(LoginDto dto) {
        //判断是否为手机号登录
        if(StrUtil.isNotBlank(dto.getPhone()) && StrUtil.isNotBlank(dto.getPassword())){
            //手机号登录
            ApUser userLogin = getOne(Wrappers.lambdaQuery(ApUser.class).eq(ApUser::getPhone, dto.getPhone()));
            if(ObjectUtil.isEmpty(userLogin)){
                throw new LeadNewsException(AppHttpCodeEnum.AP_USER_DATA_NOT_EXIST);
            }
            if (!BCrypt.checkpw(dto.getPassword(),userLogin.getPassword())){
                throw new LeadNewsException(AppHttpCodeEnum.LOGIN_PASSWORD_ERROR);
            }
            //私钥加密
            try {
                PrivateKey privateKey = RsaUtils.getPrivateKey(privateKeyPath);
                userLogin.setPassword(null);
                //生成token
                String token = JwtUtils.generateTokenExpireInMinutes(userLogin, privateKey, expire);
                Map<String, Object> resultMap = new HashMap<>();
                resultMap.put("token",token);
                resultMap.put("user",userLogin);
                return ResponseResult.okResult(resultMap);
            } catch (Exception e) {
                e.printStackTrace();
                throw new RuntimeException("获取私钥失败");
            }
        }else {
            //游客登录
            //私钥加密
            try {
                PrivateKey privateKey = RsaUtils.getPrivateKey(privateKeyPath);
                ApUser user = new ApUser();
                user.setId(0);//游客id始终为0
                //生成token
                String token = JwtUtils.generateTokenExpireInMinutes(user, privateKey, expire);
                Map<String, Object> resultMap = new HashMap<>();
                resultMap.put("token",token);
                resultMap.put("user",user);
                return ResponseResult.okResult(resultMap);
            } catch (Exception e) {
                e.printStackTrace();
                throw new RuntimeException("获取私钥失败");
            }
        }
    }
}
